<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
		<title>主动定位</title>
		<link rel="stylesheet" href="../../css/s_plus.css" />
		<link rel="stylesheet" href="../../css/base.css" />
		<link rel="stylesheet" href="../../css/layout.css" />
		<link rel="stylesheet" href="../../css/choice.css" />
		<link rel="stylesheet" href="../../css/hui.css" />

		<script type="text/javascript" src="../../libs/vue.js"></script>
		<script type="text/javascript" src="../../js/jquery.1.9.1.min.js"></script>
		<script type="text/javascript" src="../../js/app/url.js"></script>
		<script type="text/javascript" src="../../libs/hui.js"></script>
		<script type="text/javascript" src="../../js/app/api.js"></script>
		<script type="text/javascript" src="../../js/control/pop.js"></script>
		<script type="text/javascript" src="../../js/md5.js"></script>
		<script type="text/javascript" src="../../js/mui.min.js"></script>

		<!--<script type="text/javascript" src="../work/menu.js"></script>-->
		<script type="text/javascript" src="../../js/control/swal/sweetalert.min.js"></script>
		<link rel="stylesheet" href="../../js/control/swal/sweetalert.css" />

		<style>
			.amap-logo {
				bottom: -100px;
				display: none;
			}
			
			.amap-copyright {
				bottom: -100px;
				display: none;
			}
		</style>

		<body>
			<section id="wrapper" class="wrapper">
				<section class="pusher">
					<section class="sidenav sidebtn">
						<!--侧边栏-->

					</section>

					<header class="f_l maxbox subhead">
						<a class="side_arrow f_l arrow_l" data-effect="sidebtn"><i class="iconfont">&#xe600;</i></a>
						<h1>主动定位</h1>
					</header>

					<!--<div id='container'></div>
<div class="info">
    <h4 id='status'></h4><hr>
    <p id='result'></p><hr>
    <p >由于众多浏览器已不再支持非安全域的定位请求，为保位成功率和精度，请升级您的站点到HTTPS。</p>
</div>-->

					<!--<p id="allmap"></p>-->

					<section class="f_l maxbox mar_f">
						<div id="allmap"></div>
					</section>
					<section class="f_l maxbox mar_t mar_f t_c">
						<a class="btn mian_btn" id="tj">提交</a>
					</section>
				</section>
			</section>

			<!--<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.10&key=c07d645d82a5e758c9c04a102649eb30"></script>-->
			<!--<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=yBajQj79Q66U1AXKFk2oWhkYr1nlhjz4"></script>-->

			<script type="text/javascript">
				var sp = new Vue({
					el: "#wrapper",
					data: {
						address: "",
						params: {
							longitude: "",
							latitude: "",
							"userId.id": ""
						}

					},
					methods: {
						back: function() {

						},
						submitData: function() {
							alert('');
						}
					}

				});

				//动态插入公用菜单JS
				var menuJsPath = "../work/menu.js?time=";
				var randomh = Math.random();
				var e = document.getElementsByTagName("script")[0];
				var d = document.createElement("script");
				d.src = menuJsPath + randomh + "";
				d.type = "text/javascript";
				d.async = true;
				d.defer = true;
				e.parentNode.insertBefore(d, e);

				var head = $(".subhead").height();
				//var foot = $(".edit_list").height();
				var foot = +$(".t_c").height();
				var pageHeight = $(document).height();
				var mapHeight = pageHeight - (head + foot) - 40;

				$("#allmap").height(mapHeight);

				var map = new AMap.Map('allmap', {
//					  zoom: 16, //初始地图级别
					resizeEnable: true
				});
				AMap.plugin('AMap.Geolocation', function() {
					var geolocation = new AMap.Geolocation({
						enableHighAccuracy: true, //是否使用高精度定位，默认:true
						timeout: 10000, //超过10秒后停止定位，默认：5s
						buttonPosition: 'RB', //定位按钮的停靠位置
						buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
						zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点

					});
					map.addControl(geolocation);
					geolocation.getCurrentPosition(function(status, result) {
						if(status == 'complete') {
							onComplete(result)
						} else {
							onError(result)
						}
					});
				});
				//解析定位结果
				function onComplete(data) {

					//      document.getElementById('status').innerHTML='定位成功'
					console.log("定位成功");
					var str = [];

					
					
//					var gpsPoint = GPS.gcj_encrypt(data.position.getLat(), data.position.getLng());

					var zuobiao = bd_encrypt(data.position.getLng(), data.position.getLat());
					sp.params.longitude = zuobiao.bd_lng;
					sp.params.latitude = zuobiao.bd_lat;

//					var marker = new AMap.Marker({
//				    position: new AMap.LngLat(gpsPoint.lon, gpsPoint.lat),   // 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
//				    title: ''
//				});
//				
//				
//				
//				 marker.setMap(map);
//				 
//				  map.panTo([gpsPoint.lon, gpsPoint.lat]);
				 
//				map.addMarker(marker);

					//alert(JSON.stringify(sp.params));

					str.push('定位结果：' + data.position);
					str.push('定位类别：' + data.location_type);
					if(data.accuracy) {
						str.push('精度：' + data.accuracy + ' 米');
					} //如为IP精确定位结果则没有精度信息
					str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
					console.log(str.join('<br>'));
					//      document.getElementById('result').innerHTML = str.join('<br>');
				}
				//解析定位错误信息
				function onError(data) {
					//      document.getElementById('status').innerHTML='定位失败'
					//      document.getElementById('result').innerHTML = '失败原因排查信息:'+data.message;
					console.log('定位失败' + '      ' + '失败原因排查信息:' + data.message);
				}

				//百度坐标转高德（传入经度、纬度）
				function bd_decrypt(bd_lng, bd_lat) {
					var X_PI = Math.PI * 3000.0 / 180.0;
					var x = bd_lng - 0.0065;
					var y = bd_lat - 0.006;
					var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
					var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
					var gg_lng = z * Math.cos(theta);
					var gg_lat = z * Math.sin(theta);
					return {
						lng: gg_lng,
						lat: gg_lat
					}
				}
				//高德坐标转百度（传入经度、纬度）
				function bd_encrypt(gg_lng, gg_lat) {
					var X_PI = Math.PI * 3000.0 / 180.0;
					var x = gg_lng,
						y = gg_lat;
					var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
					var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
					var bd_lng = z * Math.cos(theta) + 0.0065;
					var bd_lat = z * Math.sin(theta) + 0.006;
					return {
						bd_lat: bd_lat,
						bd_lng: bd_lng
					};
				}

				$("#tj").click(function() {
					save();
										hui.loading("");

				});

				function save() {

					var path = urlManager.saveUserTrajectory();
					console.log(path);
					var username = localStorage.getItem("username");
					var sec = localStorage.getItem("sec_pwd");
					var time = apiManage.getNow();
					var key = "loginName=" + username + "&passWord=" + sec + "&time=" + time;
					var paramKey = hex_md5(key);

					var userid = localStorage.getItem("userid");
					sp.params["userId.id"] = userid;

					if(userid == null || userid == '') {
						hui.toast("userid为空");
						return;
					}

					console.log("path============================" + path);
					console.log("params===================" + JSON.stringify(sp.params));
					$.ajax({
						type: "POST",
						url: path,
						data: sp.params,
						beforeSend: function(request) {
							request.setRequestHeader("loginName", username);
							request.setRequestHeader("time", time);
							request.setRequestHeader("sign", paramKey);
						},
						success: function(result) {
							console.log("主动上传============================" + JSON.stringify(result));
							hui.closeLoading();

							swal({  
									title: "主动定位成功",
									  text: "",
									  type: "success",
									  showCancelButton: false,
									  confirmButtonColor: "#DD6B55",
									  confirmButtonText: "确认",
									  cancelButtonText: "取消",
									  closeOnConfirm: true,
									  closeOnCancel: false
								},
								function(isConfirm) {  
									if(isConfirm) {
										//				 		hui.open("terminal.html", {}, true, {});  
									} else {  }
								});

						}
					});

				}
				
				
				
				
				var GPS = {
            PI: 3.14159265358979324,
            x_pi: 3.14159265358979324 * 3000.0 / 180.0,
            delta: function (lat, lon) {
                var a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
                var ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
                var dLat = this.transformLat(lon - 105.0, lat - 35.0);
                var dLon = this.transformLon(lon - 105.0, lat - 35.0);
                var radLat = lat / 180.0 * this.PI;
                var magic = Math.sin(radLat);
                magic = 1 - ee * magic * magic;
                var sqrtMagic = Math.sqrt(magic);
                dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
                dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
                return {
                    'lat': dLat,
                    'lon': dLon
                };
            },
            //WGS-84 to GCJ-02
            gcj_encrypt: function (wgsLat, wgsLon) {
                if (this.outOfChina(wgsLat, wgsLon))
                    return {
                        'lat': wgsLat,
                        'lon': wgsLon
                    };

                var d = this.delta(wgsLat, wgsLon);
                return {
                    'lat': wgsLat + d.lat,
                    'lon': wgsLon + d.lon
                };
            },
            outOfChina: function (lat, lon) {
                if (lon < 72.004 || lon > 137.8347)
                    return true;
                if (lat < 0.8293 || lat > 55.8271)
                    return true;
                return false;
            },
            transformLat: function (x, y) {
                var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
                ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
                ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
                ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
                return ret;
            },
            transformLon: function (x, y) {
                var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
                ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
                ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
                ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
                return ret;
            }
        };
				
				
				
				
			</script>
		</body>

</html>
<!--侧边栏向左移动效果*该放在head无效-->
<script src="../../js/modernizr.custom.js"></script>
<script src="../../js/classie.js"></script>
<script src="../../js/sidebarEffects.js"></script>